<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>深入理解 Netty：构建高性能网络应用的利器</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        pre {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 8px;
            padding: 20px;
            overflow-x: auto;
            font-size: 14px;
            line-height: 1.6;
        }
        
        .feature-icon {
            width: 60px;
            height: 60px;
            display: flex;
            align-items: center;
            justify-content: center;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 12px;
            color: white;
            font-size: 24px;
            margin-bottom: 16px;
        }
        
        .timeline-item {
            position: relative;
            padding-left: 40px;
            padding-bottom: 30px;
        }
        
        .timeline-item::before {
            content: '';
            position: absolute;
            left: 10px;
            top: 8px;
            width: 12px;
            height: 12px;
            background: #667eea;
            border-radius: 50%;
            box-shadow: 0 0 0 4px rgba(102, 126, 234, 0.2);
        }
        
        .timeline-item::after {
            content: '';
            position: absolute;
            left: 15px;
            top: 20px;
            bottom: -10px;
            width: 2px;
            background: #e5e7eb;
        }
        
        .timeline-item:last-child::after {
            display: none;
        }
        
        .mermaid {
            text-align: center;
            margin: 30px 0;
        }
        
        .drop-cap {
            float: left;
            font-size: 4em;
            line-height: 0.8;
            margin: 0.1em 0.1em 0.1em 0;
            font-weight: 700;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6">
            <div class="max-w-4xl mx-auto text-center">
                <h1 class="text-5xl md:text-6xl font-bold mb-6 tracking-tight">
                    深入理解 Netty
                </h1>
                <p class="text-xl md:text-2xl mb-8 opacity-90">
                    构建高性能网络应用的利器
                </p>
                <div class="flex justify-center space-x-6 text-sm">
                    <div class="flex items-center">
                        <i class="fas fa-rocket mr-2"></i>
                        <span>异步事件驱动</span>
                    </div>
                    <div class="flex items-center">
                        <i class="fas fa-shield-alt mr-2"></i>
                        <span>高性能 NIO</span>
                    </div>
                    <div class="flex items-center">
                        <i class="fas fa-code mr-2"></i>
                        <span>简化开发</span>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto px-6 py-12">
        <!-- Introduction Section -->
        <section class="mb-16">
            <div class="content-card rounded-2xl shadow-xl p-8 md:p-12">
                <h2 class="text-3xl font-bold mb-6 section-title">什么是 Netty？</h2>
                <div class="prose prose-lg max-w-none">
                    <p class="text-gray-700 leading-relaxed mb-4">
                        <span class="drop-cap">N</span>etty 是由 JBOSS 提供的一个 Java 开源框架，它是一款异步的事件驱动的网络应用程序框架，支持快速地开发可维护的高性能的面向协议的服务器和客户端。
                    </p>
                    <p class="text-gray-700 leading-relaxed mb-4">
                        与传统的服务器不同，Netty 不需要运行在 Tomcat 这类服务器中，它可以独立构建服务器。无论是 HTTP 服务器、Socket 服务器还是 WebSocket 服务器，Netty 都能轻松胜任。
                    </p>
                    <p class="text-gray-700 leading-relaxed">
                        本质上，Netty 是对 JDK 1.4 以后提供的 NIO 的封装，它让开发者能够更专注于业务逻辑，而不必纠结于繁琐的底层实现。
                    </p>
                </div>
            </div>
        </section>

        <!-- Core Components Visualization -->
        <section class="mb-16">
            <div class="content-card rounded-2xl shadow-xl p-8 md:p-12">
                <h2 class="text-3xl font-bold mb-8 section-title">Netty 核心组件架构</h2>
                <div class="mermaid">
                    graph TB
                        A[Bootstrap/ServerBootstrap] --> B[EventLoopGroup]
                        B --> C[EventLoop]
                        C --> D[Channel]
                        D --> E[ChannelPipeline]
                        E --> F[ChannelHandler]
                        F --> G[ChannelInboundHandler]
                        F --> H[ChannelOutboundHandler]
                        E --> I[Decoder]
                        E --> J[Encoder]
                        
                        style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                        style B fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
                        style C fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                        style D fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
                        style E fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                        style F fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
                </div>
            </div>
        </section>

        <!-- Components Grid -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 text-center">核心组件详解</h2>
            <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
                <!-- Component Cards -->
                <div class="content-card rounded-xl shadow-lg p-6 hover:shadow-2xl">
                    <div class="feature-icon">
                        <i class="fas fa-plug"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">Bootstrap / ServerBootstrap</h3>
                    <p class="text-gray-600">Netty 引导组件，简化 NIO 的开发步骤，是程序的起点，负责配置和串联各个组件。</p>
                </div>

                <div class="content-card rounded-xl shadow-lg p-6 hover:shadow-2xl">
                    <div class="feature-icon">
                        <i class="fas fa-sync-alt"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">EventLoopGroup</h3>
                    <p class="text-gray-600">事件循环组，可以包含多个 EventLoop，创建时会自动创建子对象 EventLoop。</p>
                </div>

                <div class="content-card rounded-xl shadow-lg p-6 hover:shadow-2xl">
                    <div class="feature-icon">
                        <i class="fas fa-circle-notch"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">EventLoop</h3>
                    <p class="text-gray-600">事件循环，循环服务 Channel，一个 EventLoop 可以包含多个 Channel。</p>
                </div>

                <div class="content-card rounded-xl shadow-lg p-6 hover:shadow-2xl">
                    <div class="feature-icon">
                        <i class="fas fa-exchange-alt"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">Channel</h3>
                    <p class="text-gray-600">代表一个 Socket 连接或其他 IO 操作组件，是通讯的载体。</p>
                </div>

                <div class="content-card rounded-xl shadow-lg p-6 hover:shadow-2xl">
                    <div class="feature-icon">
                        <i class="fas fa-stream"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">ChannelPipeline</h3>
                    <p class="text-gray-600">传输通道，管理各种 ChannelHandler 业务控制器，提供链式管理模式。</p>
                </div>

                <div class="content-card rounded-xl shadow-lg p-6 hover:shadow-2xl">
                    <div class="feature-icon">
                        <i class="fas fa-cogs"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">ChannelHandler</h3>
                    <p class="text-gray-600">业务控制器，主要业务逻辑的实现位置，Netty 提供了许多内置的处理器。</p>
                </div>
            </div>
        </section>

        <!-- Thread Models -->
        <section class="mb-16">
            <div class="content-card rounded-2xl shadow-xl p-8 md:p-12">
                <h2 class="text-3xl font-bold mb-8 section-title">Netty 线程模型</h2>
                <div class="grid grid-cols-1 md:grid-cols-3 gap-8">
                    <div class="text-center">
                        <div class="mb-4">
                            <i class="fas fa-user text-5xl text-purple-600"></i>
                        </div>
                        <h3 class="text-xl font-bold mb-2">单线程模型</h3>
                        <p class="text-gray-600">所有 IO 操作都在同一个 NIO 线程上完成，一个线程处理连接和读写。</p>
                    </div>
                    <div class="text-center">
                        <div class="mb-4">
                            <i class="fas fa-users text-5xl text-purple-600"></i>
                        </div>
                        <h3 class="text-xl font-bold mb-2">多线程模型</h3>
                        <p class="text-gray-600">使用一组 NIO 线程（线程池）处理 IO 操作，提高并发处理能力。</p>
                    </div>
                    <div class="text-center">
                        <div class="mb-4">
                            <i class="fas fa-network-wired text-5xl text-purple-600"></i>
                        </div>
                        <h3 class="text-xl font-bold mb-2">主从多线程模型</h3>
                        <p class="text-gray-600">主线程池处理客户端连接，从线程池处理客户端读写操作。</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Implementation Examples -->
        <section class="mb-16">
            <div class="content-card rounded-2xl shadow-xl p-8 md:p-12">
                <h2 class="text-3xl font-bold mb-8 section-title">实战应用场景</h2>
                <div class="timeline-item">
                    <h3 class="text-xl font-bold mb-2">基础通信服务器</h3>
                    <p class="text-gray-600 mb-4">使用 Netty 构建高性能的 TCP/UDP 通信服务器，处理客户端连接和消息传输。</p>
                    <pre><code>// 创建主从线程池
EventLoopGroup masterGroup = new NioEventLoopGroup();
EventLoopGroup slaveGroup = new NioEventLoopGroup();

// 配置服务器
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(masterGroup, slaveGroup)
    .channel(NioServerSocketChannel.class)